package com.hzf.wxplatform.web.controller.manager.material;

import com.github.wenhao.geohash.GeoHash;
import com.hzf.api.rpc.api.ApiService;
import com.hzf.common.base.BaseController;
import com.hzf.common.util.ByteUtils;
import com.hzf.config.dao.model.ConfigRegion;
import com.hzf.config.dao.model.ConfigRegionExample;
import com.hzf.material.dao.model.*;
import com.hzf.money.dao.model.MoneyUser;
import com.hzf.ucenter.dao.model.UcenterAuthInfo;
import com.hzf.ucenter.dao.model.UcenterAuthInfoExample;
import com.hzf.ucenter.dao.model.UcenterUser;
import com.hzf.wxplatform.common.WxplatformResult;
import com.hzf.wxplatform.common.WxplatformResultConstant;
import com.hzf.wxplatform.common.utils.UcenterUserUtils;
import com.spatial4j.core.io.GeohashUtils;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/materialShop")
public class MaterialShopController extends BaseController {
    @Autowired
    ApiService apiService;
    @RequestMapping(value = "/index")
    public String index(Model model,HttpServletRequest request) {
        UcenterUser ucenterUser = new UcenterUser();
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(UcenterUserUtils.getUser(request.getSession()));
        if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        MaterialShopExample materialShopExample = new MaterialShopExample();
        materialShopExample.createCriteria().andUserIdEqualTo(ucenterUser.getId());
        MaterialShop materialShop1=apiService.selectByShop(materialShopExample);
        if(null!=materialShop1){
            model.addAttribute("materialShop",materialShop1);
            model.addAttribute("shopStatus",1);
            return thymeleaf("/CLS/CLS_SG_Manager");
        }
        model.addAttribute("shopStatus",0);
        return thymeleaf("/CLS/CLS_SG_Manager");
    }

    @RequestMapping(value = "/addShop")
    public String addShop(Model model, HttpServletRequest request) {

        HttpSession session = request.getSession();
        UcenterUser ucenterUser = new UcenterUser();
        ConfigRegion configRegion = new ConfigRegion();
        byte[] user = UcenterUserUtils.getUser(session);
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user);
        if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        byte[] address = UcenterUserUtils.getAddress(ucenterUser.getId());
        configRegion = (ConfigRegion) ByteUtils.ByteToObject(address);
        ConfigRegionExample configRegionExample  = new ConfigRegionExample();
        long type = 1;
        configRegionExample.createCriteria().andRegionTypeEqualTo(type).andRegionNumberNotEqualTo(configRegion.getRegionNumber());
        List<ConfigRegion> list = apiService.selectRegion(configRegionExample);
        MaterialListTypeExample materialListTypeExample= new MaterialListTypeExample();
        List<MaterialListType> listTypes = apiService.selectByMaterialListType(materialListTypeExample);
        for(MaterialListType materialListType :listTypes){
            MaterialListExample materialListExample = new MaterialListExample();
            materialListExample.createCriteria().andMarketTypeIdEqualTo((long)materialListType.getMarketTypeId()).andCityEqualTo(configRegion.getRegionNumber().toString());
            List<MaterialList> materialLists= apiService.selectByMaterialList(materialListExample);
            materialListType.setList(materialLists);
            for(MaterialList materialList:materialLists){
                MaterialAreaExample materialAreaExample = new MaterialAreaExample();
                materialAreaExample.createCriteria().andMarketListIdEqualTo((long)materialList.getMarketListId());
                List<MaterialArea> areas=apiService.selectByMaterialArea(materialAreaExample);

                materialList.setAreaList(areas);
            }
        }
        model.addAttribute("materialList",listTypes);
        model.addAttribute("configList",list);
        model.addAttribute("configRegion",configRegion);
        model.addAttribute("user",ucenterUser);

        return thymeleaf("/CLS/CLS_SG_TianJiaShop");
    }
    @RequestMapping(value="/shopSave",method = RequestMethod.POST)
    @ResponseBody
    private Object shopSave(MaterialShop materialShop,Model model, HttpServletRequest request){
        HttpSession session = request.getSession();
        UcenterUser ucenterUser = new UcenterUser();
        byte[] user = UcenterUserUtils.getUser(session);
        ucenterUser = (UcenterUser) ByteUtils.ByteToObject(user); if(null==ucenterUser){
            model.addAttribute("data","您的操作属于非法操作，所查看商品或设计师非您账户，请核实。");
            return  thymeleaf("/404");
        }
        materialShop.setUserId(ucenterUser.getId());
        materialShop.setShopStatus(1);
        UcenterAuthInfoExample ucenterAuthInfoExample = new UcenterAuthInfoExample();
        ucenterAuthInfoExample.createCriteria().andUserIdEqualTo(ucenterUser.getId()).andAuthTypeEqualTo(4);
        UcenterAuthInfo ucenterAuthInfo = apiService.selectUserinfo(ucenterAuthInfoExample);
        if(null==ucenterAuthInfo){
            return new WxplatformResult(WxplatformResultConstant.FAILED, "您还没有进行认证，请先认证");
        }
        materialShop.setImgUrl(ucenterAuthInfo.getOfficeDocument());
        materialShop.setShopHostName(ucenterAuthInfo.getName());
        materialShop.setShopHostTel(ucenterAuthInfo.getPhone()+"");
        MaterialShopExample materialShopExample = new MaterialShopExample();
        materialShopExample.createCriteria().andUserIdEqualTo(ucenterUser.getId());
        MaterialShop materialShop1=apiService.selectByShop(materialShopExample);
        if(null!=materialShop1){
            return new WxplatformResult(WxplatformResultConstant.FAILED, "已存在正在营业的店铺，赶快去管理你的店铺吧");
        }
        GeoHash geoHash = GeoHash.fromCoordinate(materialShop.getLat(),materialShop.getLng());
        //4024744861876082L
        long longValue = geoHash.toLong();
        materialShop.setGeoCode(longValue);

        int count= apiService.installShop(materialShop);
        if(count>0){
            return new WxplatformResult(WxplatformResultConstant.SUCCESS, "店铺添加成功，马上去管理您的店铺吧");

        }
    return  null;
    }
    @RequestMapping(value = "/getShopList",method = RequestMethod.POST)
    @ResponseBody
    public Object getShopList(Model model,HttpServletRequest servletRequest,String id){
        MaterialListTypeExample materialListTypeExample= new MaterialListTypeExample();

        List<MaterialListType> listTypes = apiService.selectByMaterialListType(materialListTypeExample);
        System.out.println(listTypes.size());
        for(MaterialListType materialListType :listTypes){
            MaterialListExample materialListExample = new MaterialListExample();
            materialListExample.createCriteria().andMarketTypeIdEqualTo((long)materialListType.getMarketTypeId()).andCityEqualTo(id);
            List<MaterialList> materialLists= apiService.selectByMaterialList(materialListExample);
            materialListType.setList(materialLists);
            for(MaterialList materialList:materialLists){
                MaterialAreaExample materialAreaExample = new MaterialAreaExample();
                materialAreaExample.createCriteria().andMarketListIdEqualTo((long)materialList.getMarketListId());
                List<MaterialArea> areas=apiService.selectByMaterialArea(materialAreaExample);
                materialList.setAreaList(areas);
            }
        }
        return new WxplatformResult(WxplatformResultConstant.SUCCESS, listTypes);

    }
}
